< previous page page_308 next page >

Page 308
' XORLine Example
' Copyright © 1998 by Desaware Inc. All Rights Reserved

Option Explicit

Dim PrevX As Single
Dim PrevY As Single
Dim centerX As Single, centerY As Single

Private Sub Form_Load()
   PrevX = -1
   PrevY = -1
   centerX = Width / 2
   centerY = Height / 2
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
   ' If same location, leave right away
   If X = PrevX And Y = PrevY Then Exit Sub

   If PrevX >= 0 Then
      ' Erases the previous line
      Line (centerX, centerY)-(PrevX, PrevY)
   End If
   Line (centerX, centerY)-(X, Y)
   PrevX = X
   PrevY = Y
End Sub
You'll see that as you move your mouse over the form, a line is drawn from the center of the form to the mouse. The routine keeps track of where the current line is, so when a new MouseMove event arrives, it can erase the previous line before drawing the new one. How does it erase the previous line? By repeating the previous drawing operation. This works because the drawing mode is set to XOR mode. This means that the pen color is applied to the form color using the XOR operation. The pen color is whiteall bits are set to 1, so the result is to toggle the value of the bits on the form as specified by the mouse movement.
Try changing the background color of the form and see how the actual line color changes.

 
< previous page page_308 next page >